/*
 * Copyright 2006-2023 ahtelit.com, All Rights Reserved.
 */

package com.telit.android.controller;

import com.telit.android.service.ImportService;
import com.telit.common.entity.DataReturn;
import com.telit.common.enums.ResultCode;
import com.telit.common.util.FileTypeCheckUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * 导入API（firecommand服务中引入easyexcel会报poi版本问题）
 *
 * @author yuezhuang
 * @since 2023-03-10
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/import", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "导入API", value = "导入API", produces = MediaType.APPLICATION_JSON_VALUE)
public class ImportController {

    private final ImportService importService;

    @PostMapping(value = "/upload/fire-point", produces = "application/json;charset=UTF-8")
    @ApiOperation("上传火烧迹地的经纬度文件")
    public DataReturn upload(@RequestParam("file") MultipartFile file) throws IOException {
        // 校验文件
        if (file == null || file.getSize() == 0) {
            return DataReturn.failure(ResultCode.BAD_REQUEST, "文件不能为空");
        }
        if (FileTypeCheckUtils.checkExcel(file.getOriginalFilename())) {
            return DataReturn.failure(ResultCode.BAD_REQUEST.getCode(), "文件格式必须为excel格式,xls和xlsx后缀名结尾");
        }

        // 将数据包两层返回给到前端
        List<List<String>> resultList = importService.getDataFormExcel(file);
        List<List<List<String>>> secondList = new ArrayList<>();
        secondList.add(resultList);
        List<List<List<List<String>>>> threeList = new ArrayList<>();
        threeList.add(secondList);

        // 存储数据
//        fireLossService.saveFirePoint(fireId,JSON.toJSONString(threeList));
        return DataReturn.ok(threeList);
    }

}
